import { DataService } from './../services/data.service';
import { Title } from '@angular/platform-browser';
import { Http } from '@angular/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot } from '@angular/router';
import '../../assets/img/kfavatar.png';
declare var $: any;

@Component({
  selector: 'app-shared-goods',
  templateUrl: './shared-goods.component.html',
  styleUrls: ['./shared-goods.component.css']
})
export class SharedGoodsComponent implements OnInit {

  constructor(public DataService: DataService,
    public titleSet: Title) { }
  /**订单id */
  public orderId: string;
  /**用户id */
  public userId: string;
  /**起始地 */
  public startingPlaceName: string;
  /**目的地 */
  public destinationName: string;
  /** 货物名称 数量*/
  public goodsInfo: string;
  /**订单编号 */
  public orderNumber: string;
  /**车长 */
  public carLength: string;
  /**路程  */
  public distance: string;
  /**客服姓名 */
  public serviceName: string;
  public contacts: string;
  /**客服电话 */
  public servicePhone: string;
  /**去空格客服电话 */
  public servicePhoneK: string;
  /**客服头像 */
  public serviceAvatar: string;
  public dispatcherHead: string;
  /**是否为ios */
  public iosA: boolean;
  /**是否为android */
  public andoridA: boolean;
  /**发货时间 */
  public deliveryTime: string;
  /**到货时间 */
  public arrivalTime: string;
  /**货物数量 */
  public amount: number;
  /**货物数量单位 */
  public amountUnit: string;
  /**货物类型 */
  public category: string;
  /**订单备注 */
  public remarks: string;
  /**是否显示按钮 */
  public qdshow: boolean;
  /**是否显示备注 */
  public reshow: boolean;
    /** 订单所需车型 15 拖车（集装箱）*/
    public vehicleType :number;
    /** 目的地详细地址*/
    public destinationPlaceDetailName :string;

  ngOnInit() {
    /**页面名称 */
    this.titleSet.setTitle('来货拉货源分享');
    this.startingPlaceName = "暂无数据";
    this.destinationName = "暂无数据";
    this.deliveryTime = "暂无数据";
    this.arrivalTime = "暂无数据";
    this.category = "暂无数据";
    this.amount = 0;
    this.amountUnit = "暂无数据";
    this.orderNumber = "暂无数据";
    this.carLength = "暂无数据";
    this.remarks = "暂无数据";
    this.serviceName = "暂无数据";
    this.servicePhone = "暂无数据";
    this.distance = "暂无数据";
    this.reshow = false;
    this.qdshow = false;

    this.iosA = false;
    this.andoridA = true;

    this.serviceAvatar = "../assets/img/kfavatar.png";
    /**地址栏参数 */
    var pathUrl = window.location.hash;
    /**中文转码 */
    var zhuanma = decodeURIComponent(pathUrl);
    /**匹配？ */
    var wenhao = zhuanma.indexOf("?");

    if (wenhao !== -1) {
      /**？的位置 */
      var pathu = zhuanma.substring(wenhao + 1);
      /**分割成数组 */
      var urlarr = pathu.split("&");
      this.orderId = urlarr[0].split("=")[1];
      this.userId = urlarr[1].split("=")[1];
      /**路程 */
      var disluc = urlarr[2].split("=")[1];
      if (disluc == "null" || disluc == "" || disluc == "(null)" || disluc == undefined) {
        this.distance = "0";
      } else {
        this.distance = disluc;
      }
      /** 签名*/
      var signature = urlarr[3].split("=")[1];
      /** 时间戳*/
      var timestamp = urlarr[4].split("=")[1];
      /** 4位数*/
      var nonce = urlarr[5].split("=")[1];
      /**loading */
      $.showLoading();
      /**货源分享 */
      this.DataService.SharedGoods(this.orderId, this.userId)
        .subscribe(
          data => {
            $.hideLoading();
            this.reshow = true;
            if (data.startingPlaceName === undefined) {
              this.distance = "暂无数据";
            }else{
              this.qdshow = true;
            }
            this.startingPlaceName = data.startingPlaceName;
            
            if(data.vehicleType == 15){
              this.destinationName = data.destinationPlaceDetailName;
            }else{
              this.destinationName = data.destinationName;
            }
            var delitime = new Date(data.deliveryTime);
            var arrtime = new Date(data.arrivalTime);
            this.deliveryTime = data.deliveryTime.substr(0, 10);
            this.arrivalTime = data.arrivalTime.substr(0, 10);
            this.category = data.category;
            this.amount = data.amount;
            this.amountUnit = data.amountUnit;
            this.orderNumber = data.orderNumber;
            this.carLength = data.carLength;
            this.remarks = data.remarks;
            if (this.remarks == "") {
              this.reshow = false;
            }
            this.serviceName = data.contacts;
            this.servicePhone = data.contactPhoneNumber;
            this.servicePhoneK = this.servicePhone.replace(/\s+/g, "");
            /**客服头像判断 */
            var sAvatar = data.dispatcherHead;
            if (sAvatar == "null" || sAvatar == "" || sAvatar == "(null)" || sAvatar == undefined) {
              this.serviceAvatar = "../assets/img/kfavatar.png";
            } else {
              this.serviceAvatar = data.dispatcherHead;
            }
          }
          // ,
          // error => {
          //   // console.error(error);
          //   $.alert(error.errorMessage);
          // }
        );

    } else {
      $.alert("非法访问~填充为示例数据。");
    }

  }
  /**格式化日期 时区GMT+8*/
  dateFtt(fmt, date) { //author: meizz
    var o = {
      "M+": date.getMonth() + 1,//月份
      "d+": date.getDate(),//日
      "h+": date.getHours(),//小时
      "m+": date.getMinutes(),//分
      "s+": date.getSeconds(),//秒
      "q+": Math.floor((date.getMonth() + 3) / 3),//季度  
      "S": date.getMilliseconds()//毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }

  /**拨号打电话 */
  sTell() {
    window.open('tel:' + this.servicePhoneK);
  }
  /**关闭提示 */
  sclose() {
    $.closePopup();
  }

  /**抢单报价按钮 */
  sQdbj() {

    /** 通过JS页面唤醒app（安卓+ios）*/
    var browser = {
      versions: function () {
        var u = navigator.userAgent,
          app = navigator.appVersion;
        // alert(u);
        return {
          trident: u.indexOf('Trident') > -1,
          /*IE内核*/
          presto: u.indexOf('Presto') > -1,
          /*opera内核*/
          webKit: u.indexOf('AppleWebKit') > -1,
          /*苹果、谷歌内核*/
          gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,
          /*火狐内核*/
          mobile: !!u.match(/AppleWebKit.*Mobile.*/),
          /*是否为移动终端*/
          ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
          /*ios终端*/
          android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
          /*android终端或者uc浏览器*/
          iPhone: u.indexOf('iPhone') > -1,
          /*是否为iPhone或者QQHD浏览器*/
          iPad: u.indexOf('iPad') > -1,
          /*是否iPad*/
          webApp: u.indexOf('Safari') == -1,
          /*是否web应该程序，没有头部与底部*/
          souyue: u.indexOf('souyue') > -1,
          superapp: u.indexOf('superapp') > -1,
          /**微信浏览器 */
          weixin: u.toLowerCase().indexOf('micromessenger') > -1,
          Safari: u.indexOf('Safari') > -1,
          myqq: navigator.userAgent.indexOf('QQ') > -1,
          /**uc浏览器 */
          UCBrowser: navigator.userAgent.indexOf('UCBrowser') > -1
        };
      }()

      // language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };
    /**微信/qq */
    if (browser.versions.weixin) {
      if (browser.versions.ios) {
        this.iosA = true;
        this.andoridA = false;
      }
      else {
        this.iosA = false;
        this.andoridA = true;
      }
      /**显示提示 */
      $("#appOpen").popup();
    }
    else if (browser.versions.ios) {
      if (browser.versions.myqq) {
        this.iosA = true;
        this.andoridA = false;
        /**显示提示 */
        $("#appOpen").popup();
      } else {
        // window.open("laihuola://&" + this.orderId);
        window.location.href = "laihuola://&" + this.orderId;
        setTimeout(function () {
          // window.location.href = "http://www.vip56.cn/shared.html";
          top.location.href = "http://www.vip56.cn/shared.html";  //要加两遍      
        }, 2000)
      }

    } else if (browser.versions.android) {
      if (browser.versions.UCBrowser) {
        window.location.href = "laihuola://www.laihuola.com/share/goods?orderId=" + this.orderId;
        // alert("laihuola://www.laihuola.com/share/goods?orderId=" + this.orderId);
        setTimeout(function () {
          window.location.href = "http://www.vip56.cn/shared.html";
        }, 2000)
      } else {
        window.open("laihuola://www.laihuola.com/share/goods?orderId=" + this.orderId);
        // alert("laihuola://www.laihuola.com/share/goods?orderId=" + this.orderId);
        setTimeout(function () {
          window.location.href = "http://www.vip56.cn/shared.html";
        }, 2000)
      }

    }


  }


}
